from flask import Blueprint, request, render_template, flash, redirect, url_for
from services.adminuser_service import AdminUserService

# 创建蓝图
admin_bp = Blueprint('admin', __name__)

# 查询所有用户
@admin_bp.route('/login', methods=['GET'])
def login():
    return render_template('login.html')


@admin_bp.route('/login', methods=['POST'])
def check_login():
    username = request.form['username']
    password = request.form['password']


    result = AdminUserService.login(username, password)

    if not result:
        flash("用户名或密码错误，请重试！", "error")  # 登录失败，发送错误消息
        return redirect(url_for('admin.login'))

    return redirect(url_for('admin.index'))


@admin_bp.route('/index', methods=['GET'])
def index():
    return render_template('index.html')